package com.sgb.kjwl.viewmodel.home;

import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.api.APIManager;
import com.swgk.core.base.api.ICallBack;
import com.sgb.kjwl.model.entity.LeaseSearchResultEntity;
import com.sgb.kjwl.model.entity.ProListEntity;
import com.sgb.kjwl.model.entity.RecommendEntity;
import com.sgb.kjwl.model.entity.StoreEntity;
import com.sgb.kjwl.model.entity.home.SearchGoodsSummaryEntity;
import com.sgb.jicai.entity.BaseCount;
import com.sgb.kjwl.model.entity.pick.PickSearchType;
import com.sgb.kjwl.model.entity.request.LeaseSearchFilterJson;
import com.sgb.kjwl.model.response.ListResponse2;
import com.sgb.kjwl.repository.HomeRepertory;
import com.sgb.kjwl.view.ui.fragment.home.SearchNewFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.SharedPreferenceUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class SearchNewFragModel extends BaseViewModel {

    private SearchNewFragment fragment;
    private HomeRepertory repertory;
    public LeaseSearchFilterJson filterJson = new LeaseSearchFilterJson();

    private Map map;
    private int limit = 10;

    public SearchNewFragModel(SearchNewFragment fragment, HomeRepertory repertory) {
        this.fragment = fragment;
        this.repertory = repertory;
    }

    public void searchCompanyList(String searchType, String keyWord, int page, boolean isLoadMore) {
        map = new HashMap();
        map.put("keyword", keyWord);
        map.put("pageNum", page);
        map.put("pageSize", limit);
        map.put("areaCode", repertory.getAreaCode());
        map.put("code", searchType);
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据获取中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getBigHomeAPI().searchCompany(map).enqueue(new Callback<BaseEntity<ProListEntity<List<StoreEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ProListEntity<List<StoreEntity>>>> call, Response<BaseEntity<ProListEntity<List<StoreEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (response.body().getData() != null) {
                        if (response.body().getData().isLastPage()) {
                            fragment.showCompanyList(response.body().getData().getList(), isLoadMore, true);
                        } else {
                            fragment.showCompanyList(response.body().getData().getList(), isLoadMore, false);
                        }
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ProListEntity<List<StoreEntity>>>> call, Throwable t) {
                t.printStackTrace();
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void getGoodsSummary(String key) {
        map = new HashMap();
        map.put("page", "1");
        map.put("limit", limit);
        map.put("key", key);
        map.put("item_provinceId", repertory.getAreaCode());
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据获取中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getBigHomeAPI().getGoodsSummary(map).enqueue(new Callback<BaseEntity<SearchGoodsSummaryEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<SearchGoodsSummaryEntity>> call, Response<BaseEntity<SearchGoodsSummaryEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    fragment.setGoodsSummary(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<SearchGoodsSummaryEntity>> call, Throwable t) {
                t.printStackTrace();
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void searchGoodsList(String searchType, String keyWord, String item_cid, int page, boolean isLoadMore) {
        map = new HashMap();
        map.put("keyword", keyWord);
        map.put("pageNum", page);
        map.put("pageSize", limit);
        map.put("areaCode", repertory.getAreaCode());
        map.put("code", searchType);
        if ("0".equals(item_cid)) {
            item_cid = "";
        }
        map.put("item_cid", item_cid);
        map.put("lon", "".equals(SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LONGITUDE, ""))
                ? repertory.getLatitude() : SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LONGITUDE, ""));
        map.put("lat", "".equals(SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LATITUDE, ""))
                ? repertory.getLatitude() : SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LATITUDE, ""));
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据获取中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getBigHomeAPI().searchGoods(map).enqueue(new Callback<BaseEntity<ProListEntity<List<RecommendEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ProListEntity<List<RecommendEntity>>>> call, Response<BaseEntity<ProListEntity<List<RecommendEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (response.body().getData() != null) {
                        if (response.body().getData().isLastPage()) {
                            fragment.showGoodsList(response.body().getData().getList(), isLoadMore, true);
                        } else {
                            fragment.showGoodsList(response.body().getData().getList(), isLoadMore, false);
                        }
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ProListEntity<List<RecommendEntity>>>> call, Throwable t) {
                t.printStackTrace();
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void getPickSummary(String keyword) {
        map = new HashMap();
        map.put("areacode", repertory.getAreaCode());
        map.put("keyword", keyword);
        DialogHelper.showProgressDialog(fragment.getContext(), null, "请求中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().getfindMenuListData(map).enqueue(new ICallBack<BaseEntity<PickSearchType>>() {
            @Override
            public void onSuccess(BaseEntity<PickSearchType> data) {
                if (data == null) return;
                if (data.getData() == null) return;
                if (data.getState().equals(AppConstant.REQEUST_SECCUESS)) {
                    fragment.showPickSummary(data.getData());
                }
            }
        });
    }

    //获取需方发起数量
    public void getXuFangCount(String words) {
        APIManager.getInstance().getCentralizedMiningAPI().getXuFangCount(words).enqueue(new ICallBack<BaseEntity<BaseCount>>() {
            @Override
            public void onSuccess(BaseEntity<BaseCount> data) {
                if (data == null || data.getState() == null) {
                    return;
                }
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    getFactoryCount(words, data.getData().getCommon());
                }
            }
        });
    }

    //获取供方发起数量
    public void getFactoryCount(String words, int common) {
        APIManager.getInstance().getCentralizedMiningAPI().getFactoryCount(words).enqueue(new ICallBack<BaseEntity<Integer>>() {
            @Override
            public void onSuccess(BaseEntity<Integer> data) {
                if (data == null || data.getState() == null) {
                    return;
                }
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                   fragment.showCollectSummary(common, data.getData());
                }
            }
        });
    }

    //租赁搜索
    public void getLeaseHomeSearch(String rentType,String keyword, int page) {
        DialogHelper.showProgressDialog(fragment.getActivity(), "加载中...").setCanceledOnTouchOutside(false);
        map = new HashMap();
        map.put("current",page);
        map.put("sortWay","-1");
        map.put("limit",limit);
        map.put("keyword",keyword);
        map.put("rentType",rentType);
        map.put("firstCategoryNo","");
        map.put("type","");

        APIManager.getInstance().getLeaseAPI().getLeaseHomeSearchs(map)
                .enqueue(new Callback<BaseEntity<ListResponse2<List<LeaseSearchResultEntity>>>>() {
                    @Override
                    public void onResponse(Call<BaseEntity<ListResponse2<List<LeaseSearchResultEntity>>>> call, Response<BaseEntity<ListResponse2<List<LeaseSearchResultEntity>>>> response) {
                        DialogHelper.dismissProgressDialog();
                        if (response.body() == null || response.body().getState() == null) {
                            return;
                        }
                        if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                            if (response.body().getData() != null) {
                                getLeaseHomeSearch2("2", keyword, page, response.body().getData().getTotal());
                            }
                        }

                    }

                    @Override
                    public void onFailure(Call<BaseEntity<ListResponse2<List<LeaseSearchResultEntity>>>> call, Throwable t) {
                        DialogHelper.dismissProgressDialog();
                    }
                });
    }

    public void getLeaseHomeSearch2(String rentType,String keyword, int page, int common) {
        DialogHelper.showProgressDialog(fragment.getActivity(), "加载中...").setCanceledOnTouchOutside(false);
        map = new HashMap();
        map.put("current",page);
        map.put("sortWay","-1");
        map.put("limit",limit);
        map.put("keyword",keyword);
        map.put("rentType",rentType);
        map.put("firstCategoryNo","");
        map.put("type","");

        APIManager.getInstance().getLeaseAPI().getLeaseHomeSearchs(map)
                .enqueue(new Callback<BaseEntity<ListResponse2<List<LeaseSearchResultEntity>>>>() {
                    @Override
                    public void onResponse(Call<BaseEntity<ListResponse2<List<LeaseSearchResultEntity>>>> call, Response<BaseEntity<ListResponse2<List<LeaseSearchResultEntity>>>> response) {
                        DialogHelper.dismissProgressDialog();
                        if (response.body() == null || response.body().getState() == null) {
                            return;
                        }
                        if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                            if (response.body().getData() != null) {
                                fragment.getRentSummary(common, response.body().getData().getTotal());
                            }
                        }

                    }

                    @Override
                    public void onFailure(Call<BaseEntity<ListResponse2<List<LeaseSearchResultEntity>>>> call, Throwable t) {
                        DialogHelper.dismissProgressDialog();
                    }
                });
    }
}
